#include "Skydome.h"

Skydome::Skydome()
:Entity()
{

}

Skydome::~Skydome()
{

}

FWint Skydome::Init()
{
	FWint err = 0;

	_Mesh = new Mesh(1, "Resources\\Meshes\\", "skydome.snk");
	err += _Mesh->Init();

	_Texture = new Texture();
	char** tex = new char*[6];
	tex[0] = StringHelper::SumString(BASE_SRC_PATH, "Resources\\Textures\\Skydome_0.tga");
	tex[1] = StringHelper::SumString(BASE_SRC_PATH, "Resources\\Textures\\Skydome_1.tga");
	tex[2] = StringHelper::SumString(BASE_SRC_PATH, "Resources\\Textures\\Skydome_2.tga");
	tex[3] = StringHelper::SumString(BASE_SRC_PATH, "Resources\\Textures\\Skydome_3.tga");
	tex[4] = StringHelper::SumString(BASE_SRC_PATH, "Resources\\Textures\\Skydome_4.tga");
	tex[5] = StringHelper::SumString(BASE_SRC_PATH, "Resources\\Textures\\Skydome_5.tga");
	err += _Texture->Init(6, tex, GL_TEXTURE_CUBE_MAP);
	SAFE_DEL_ARRAY_POINTER(6, tex);

	_Shader = new Shader(2, "Resources\\Shaders\\", "skydome.vsh;skydome.fsh");
	err += _Shader->Init();

	return err;
}

void Skydome::Update(FWfloat deltaTime)
{
	//SetRotateY(0.0005f);
	Entity::Update(deltaTime);
}

void Skydome::Draw()
{
	glUseProgram(_Shader->ProgramObject);

	if (_Shader->CubeSampler != -1)
	{
		glBindTexture(GL_TEXTURE_CUBE_MAP, _Texture->TBOs[0]);
		glUniform1i(_Shader->CubeSampler, 0);
	}

	glBindBuffer(GL_ARRAY_BUFFER, _Mesh->VBOs[0]);

	if (_Shader->Position != -1)
	{
		glEnableVertexAttribArray(_Shader->Position);
		glVertexAttribPointer(_Shader->Position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
	}
	if (_Shader->Coordinate != -1)
	{
		glEnableVertexAttribArray(_Shader->Coordinate);
		glVertexAttribPointer(_Shader->Coordinate, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3));
	}
	if (_Shader->MatrixWVP != -1)
	{
		Matrix WVP = m_LocalWorld * Global::CurrentCamera->View * Global::CurrentCamera->Projection;
		glUniformMatrix4fv(_Shader->MatrixWVP, 1, GL_FALSE, (FWfloat*)&WVP);
	}

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _Mesh->IBOs[0]);
	glDrawElements(GL_TRIANGLES, _Mesh->IBOCount, GL_UNSIGNED_INT, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}

void Skydome::Destroy()
{
	_Mesh->Destroy();
	_Texture->Destroy();
	_Shader->Destroy();
}